package netcracker;
 
/**
 *
 * @author Galimov, Metsker
 */
public class DwellingFloor {
    private Flat[] flats;
    
    public DwellingFloor(int flatQuantity){
        flats = new Flat[flatQuantity];
        for (int i = 0; i < flats.length; i++){
            flats[i] = new Flat();
        }
    }
    
    public DwellingFloor(Flat[] flats){
        this.flats = new Flat[flats.length];
        System.arraycopy(flats, 0, this.flats, 0, flats.length);
    }
    
    public int flatCounter(){
        return flats.length;
    }
    
    public double getFloorSquare(){
        double square = 0;
        for (Flat f:flats)
            square += f.getSquare();
        return square;
    }
    
    public int getFloorRooms(){
        int rooms = 0;
        for (Flat f:flats)
            rooms += f.getRooms();
        return rooms;
    }
    
    public Flat[] getFloor(){
        return flats; 
    }
    
    public Flat getFlat(int num){
        return flats[num];
    }
    
    public void editFlat(int num, Flat flat){
        this.flats[num].setRooms(flat.getRooms());
        this.flats[num].setSquare(flat.getSquare());
    }
    
    public void addFlat(int num){
        if (num >= flats.length){
            num = flats.length;
        }
        Flat[] tempFlats = new Flat[flats.length+1];
        System.arraycopy(flats, 0, tempFlats, 0, num);
        System.arraycopy(flats, num, tempFlats, num+1, flats.length-num);
        tempFlats[num] = new Flat();
        flats = tempFlats;
    }
    
    public void addFlat(int num, Flat flat){
        if (num >= flats.length){
            num = flats.length;
        }
        Flat[] tempFlats = new Flat[flats.length+1];
        System.arraycopy(flats, 0, tempFlats, 0, num);
        System.arraycopy(flats, num, tempFlats, num+1, flats.length-num);
        tempFlats[num] = flat;
        flats = tempFlats;
    }
    
    public void removeFlat(int num){
        Flat[] tempFlats = new Flat[flats.length-1];
        System.arraycopy(this.flats, 0, tempFlats, 0, num);
        System.arraycopy(this.flats, num+1, tempFlats, num, this.flats.length-num-1);
        this.flats = tempFlats;
    }
    
    public Flat getBestSpace(){
        double bestSpace = 0;
        int num = 0;
        for (int i = 0; i < flats.length; i++){
            if (flats[i].getSquare() > bestSpace){
                bestSpace = flats[i].getSquare();
                num = i;
            }
        }
        return flats[num];                
    }
    
    public void showDwellingFloor(){
       for (int i = 0; i < this.flatCounter(); i++)
            System.out.println("№:" + i + ", square: " + flats[i].getSquare() + ", rooms: " + flats[i].getRooms());
    }
}
